using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;

namespace Meta
{
	/// <summary>
	/// Summary description for ControlPanel.
	/// </summary>
	public class ControlPanel : System.Windows.Forms.Form
	{
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;

		public ControlPanel(string name)
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();

			//
			// TODO: Add any constructor code after InitializeComponent call
			//
			Name = name;
		}

		public static ControlPanel MakeControlPanel(string name) {
			return (ControlPanel)Meta.TopLevel.LaunchForm(typeof(ControlPanel), new object[1] { name });
		}

		private static ControlPanel defaultControlPanel;

		public static ControlPanel Default {
			get {
				lock (typeof(ControlPanel)) {
					if (defaultControlPanel == null || !defaultControlPanel.Visible)
						defaultControlPanel = MakeControlPanel("Control Panel");
					return defaultControlPanel;
				}
			}
		}

		class ControlComparer : IComparer {
			public static ControlComparer compare = new ControlComparer();
			int IComparer.Compare(object a, object b) {
				Control ac = (Control) a;
				Control bc = (Control)b;

				return String.Compare(ac.Name, bc.Name);
			}
		}

		public void Add(Control control) {
			Controls.Add(control);

			SuspendLayout();

			// Compute a list of all controls, sorted by Name.
			Control[] currentControls = new Control[Controls.Count];
			Controls.CopyTo(currentControls,0);
			Array.Sort(currentControls, ControlComparer.compare);

			// Now lay them out in alphabetical order.
			int ypos = 0;
			foreach (Control c in currentControls) {
				c.Top = ypos;
				c.Left = 0;
				ypos += c.Height;
			}
			this.Height = ypos+SystemInformation.CaptionHeight+2*SystemInformation.FrameBorderSize.Height+2;
			ResumeLayout();
		}

		delegate Control AddDelegate(Type t, object[] args); 
		public Control Add(Type controlType, object[] constructorArgs) {
			if (InvokeRequired)
				return (Control)Invoke(new AddDelegate(Add), new object[2] { controlType, constructorArgs });
			else {
				Control c = (Control)controlType.InvokeMember(null, System.Reflection.BindingFlags.CreateInstance, null, null, constructorArgs);

				// Finally, we have the damn thing.
				Add(c);
				return c;
			}
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(ControlPanel));
			// 
			// ControlPanel
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(392, 0);
			this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
			this.Name = "ControlPanel";
			this.Text = "ControlPanel";

		}
		#endregion
	}
}
